package com.carkeeper.employee.controller;

import com.carkeeper.employee.service.EmployeeService;
import com.carkeeper.entity.PageResult;
import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.pojo.Department;
import com.carkeeper.pojo.Employee;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    /**
     * 查询所有
     */
    @GetMapping
    public Result findAll(){

        List<Employee> employeeList = employeeService.findAll();

        return new Result(true,StatusCode.OK,"查询成功",employeeList);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result<Employee> findById(@PathVariable("id") Integer id){

        Employee idList = employeeService.findById(id);

        return new Result<>(true,StatusCode.OK,"查询成功",idList);
    }

    /**
     * 分页条件查询
     *
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestParam Map searchMap, @PathVariable int page, @PathVariable  int size){
        Page<Employee> pageList = employeeService.findPage(searchMap, page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }
    @PostMapping
    public Result add(@RequestBody Employee employee){

        employeeService.add(employee);

        return new Result(true,StatusCode.OK,"增加成功");

    }
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Employee employee,@PathVariable Integer id){

        employee.setId(id);

        employeeService.update(employee);

        return new Result(true,StatusCode.OK,"修改成功");

    }

    /**
     * 锁定状态
     */
    @GetMapping("/lockBlock/{id}")
    public Result<Employee>  lockBlock(@PathVariable Integer id){

        Employee employee = employeeService.lockBlock(id);

        return new Result<>(true,StatusCode.OK,"修改成功",employee);
    }

    @GetMapping("/showList")
    public Result<List> showList(){

        List list = employeeService.showList();

        return new Result<>(true,StatusCode.OK,"",list);
    }
}
